package com.example.yt_app;

import android.app.Activity;
import android.content.Context;

import android.view.View;
import android.view.inputmethod.InputMethodManager;
import java.util.Stack;

/**
 * @author AppManager
 * @date 2019-04-23 13:46
 */
public class AppManager {

    protected Stack<Activity> mActivities;

    {
        mActivities = new Stack<>();
    }

    private static class Holder {
        private static final AppManager INSTANCE = new AppManager();
    }

    public static AppManager getInstance() {
        return Holder.INSTANCE;
    }


    public void addActivity(Activity activity) {
        System.out.println("添加成功");
        mActivities.add(activity);
    }

    public void removeActivity(Activity activity) {
        hideSoftKeyBoard(activity);
        activity.finish();
        mActivities.remove(activity);
        System.out.println("删除成功");
    }

    public void removeAllActivity() {
        for (Activity activity : mActivities) {
            hideSoftKeyBoard(activity);
            activity.finish();
        }
        mActivities.clear();
    }

    protected void hideSoftKeyBoard(Activity activity) {
        View localView = activity.getCurrentFocus();
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (localView != null && imm != null) {
            imm.hideSoftInputFromWindow(localView.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }
}
